package net.liteheaven.dtimer.misc

import android.os.Parcelable

import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.ObjectInputStream
import java.io.ObjectOutputStream

/**
 * Created by LiangYu on 2018/7/30.
 */
object ParcelableUtil {

    fun parcelable2ByteArray(parcelable: Parcelable): ByteArray? {
        try {
            val bao = ByteArrayOutputStream()
            val oos = ObjectOutputStream(bao)
            oos.writeObject(parcelable)
            oos.flush()
            return bao.toByteArray()
        } catch (e: Exception) {
            // ignore
        }

        return null
    }

    fun byteArray2Parcelable(byteArray: ByteArray): Parcelable? {
        try {
            val bai = ByteArrayInputStream(byteArray)
            val ois = ObjectInputStream(bai)
            return ois.readObject() as Parcelable
        } catch (e: Exception) {
            // ignore
        }

        return null
    }
}
